// pages/staffCountSetting/index.js
const { appointmentConfigApi } = require("../../utils/api.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    staffCount: 1,
    loading: false,
    maxCount: 10, // 最大美甲师人数限制
    quickOptions: [1, 2, 3, 4, 5, 6], // 快速选择选项
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.loadStaffCount();
  },

  /**
   * 加载美甲师人数
   * @param {boolean} showLoading - 是否显示加载提示
   */
  async loadStaffCount(showLoading = true) {
    try {
      if (showLoading) {
        wx.showLoading({
          title: "加载中...",
        });
      }

      const result = await appointmentConfigApi.getGlobalStaffCount();
      if (result.code === 0) {
        const count = result.data.staff_count || 1;
        this.setData({
          staffCount: count,
        });
      } else {
        wx.showToast({
          title: result.message || "加载失败",
          icon: "none",
        });
      }
    } catch (error) {
      console.error("加载美甲师人数失败:", error);
      wx.showToast({
        title: "加载失败",
        icon: "none",
      });
    } finally {
      if (showLoading) {
        wx.hideLoading();
      }
    }
  },

  /**
   * 下拉刷新
   */
  async onPullDownRefresh() {
    try {
      // 刷新数据，不显示加载提示（下拉刷新本身有动画）
      await this.loadStaffCount(false);
    } catch (error) {
      console.error("刷新失败:", error);
    } finally {
      // 停止下拉刷新动画
      wx.stopPullDownRefresh();
    }
  },

  /**
   * 输入美甲师人数
   */
  onStaffCountInput(event) {
    let value = parseInt(event.detail.value) || 1;
    const { maxCount } = this.data;
    
    // 限制范围
    if (value <= 0) {
      value = 1;
    } else if (value > maxCount) {
      value = maxCount;
      wx.showToast({
        title: `最多只能设置${maxCount}个美甲师`,
        icon: "none",
      });
    }
    
    this.setData({
      staffCount: value,
    });
  },

  /**
   * 快速选择美甲师人数
   */
  onQuickSelect(e) {
    const count = e.currentTarget.dataset.count;
    this.setData({
      staffCount: count,
    });
  },

  /**
   * 减少美甲师人数
   */
  onDecrease() {
    const { staffCount } = this.data;
    if (staffCount > 1) {
      this.setData({
        staffCount: staffCount - 1,
      });
    } else {
      wx.showToast({
        title: "美甲师人数至少为1",
        icon: "none",
      });
    }
  },

  /**
   * 增加美甲师人数
   */
  onIncrease() {
    const { staffCount, maxCount } = this.data;
    if (staffCount < maxCount) {
      this.setData({
        staffCount: staffCount + 1,
      });
    } else {
      wx.showToast({
        title: `最多只能设置${maxCount}个美甲师`,
        icon: "none",
      });
    }
  },

  /**
   * 保存美甲师人数
   */
  async saveStaffCount() {
    const { staffCount } = this.data;

    if (staffCount <= 0) {
      wx.showToast({
        title: "美甲师人数必须大于0",
        icon: "none",
      });
      return;
    }

    // 确认对话框
    wx.showModal({
      title: "确认保存",
      content: `确定要将美甲师人数设置为 ${staffCount} 人吗？\n\n设置后，每个时间段可同时预约 ${staffCount} 个客户。`,
      confirmText: "确认保存",
      cancelText: "取消",
      success: async (res) => {
        if (res.confirm) {
          await this.doSave();
        }
      },
    });
  },

  /**
   * 执行保存操作
   */
  async doSave() {
    const { staffCount } = this.data;

    try {
      this.setData({ loading: true });
      wx.showLoading({
        title: "保存中...",
      });

      const result = await appointmentConfigApi.setGlobalStaffCount(staffCount);
      if (result.code === 0) {
        wx.showToast({
          title: "保存成功",
          icon: "success",
        });
        // 延迟返回上一页
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      } else {
        wx.showToast({
          title: result.message || "保存失败",
          icon: "none",
        });
      }
    } catch (error) {
      console.error("保存美甲师人数失败:", error);
      wx.showToast({
        title: "保存失败",
        icon: "none",
      });
    } finally {
      wx.hideLoading();
      this.setData({ loading: false });
    }
  },
});
